<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Resources\Admin\PaperResource;
use App\Models\Option;
use App\Models\Paper;
use App\Models\Question;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PaperController extends BaseController
{
    public function index(Request $request): JsonResponse
    {
        $perPage = $request->integer('per_page', 20);
        $query = Paper::query()->orderByDesc('id');
        $title = $request->string('title');
        if ($title->isNotEmpty()) {
            $query->where('title', 'like', '%' . $title . '%');
        }
        $createTimeArr = $request->input('create_time');
        if ($createTimeArr && count($createTimeArr) > 1) {
            $query->whereNotNull('created')
                ->where('created', '>=', $createTimeArr[0] . ' 00:00:00')
                ->where('created', '<=', $createTimeArr[1] . ' 23:59:59');
        }
        $isOffline = $request->input('is_offline');
        if (isset($isOffline) && $isOffline != '' && $isOffline != -1) {
            $query->where('is_offline', $isOffline);
        }
        $platform = $request->input('platform');
        if (isset($platform) && $platform != '') {
            $query->where('platform', $platform);
        }
        $list = $query->paginate($perPage);
        return $this->success(PaperResource::collection($list));
    }

    public function show(Request $request): JsonResponse
    {
        $id = $request->input('id');
        $paper = Paper::query()
            ->find($id);
        return $this->success(PaperResource::make($paper));
    }

    public function store(Request $request): JsonResponse
    {
        $rules = [
            'title' => 'required',
        ];
        $messages = [
            'title.required' => '请输入问卷标题',
        ];
        $error = $this->validatorStopOnFirst($request, $rules, $messages);
        if ($error) {
            return $this->error($error);
        }
        $id = $request->integer('id');
        if ($id) {
            $paper = Paper::find($id);
            if (!$paper) {
                return $this->error('问卷不存在');
            }
        } else {
            $paper = new Paper();
        }
        $paper->title = $request->input('title');
        $paper->desc = $request->input('desc');
        $paper->save();

        $topic = $request->input('topic') ?? [];
        if ($topic) {
            foreach ($topic as $item) {
                $question = new Question();
                $question->paper_id = $paper->id;
                $question->title = $item['title'];
                $question->type = $item['type'];
                $question->is_required = $item['isRequired'];
                $question->desc = $item['desc'];
                $question->save();

                $options = $item['options'] ?? [];
                $index = 0;
                foreach ($options as $optionItem) {
                    $option = new Option();
                    $option->paper_id = $paper->id;
                    $option->question_id = $question->id;
                    $option->value = $optionItem['value'];
                    $option->desc = $optionItem['desc'];
                    $option->sort = $index;
                    $option->save();
                    $index++;
                }
            }
        }
        return $this->success();
    }

    public function destroy(int $id)
    {
        $pager = Paper::find($id);
        if (!$pager) {
            return $this->error('未找到数据');
        }
        $pager->delete();
        return $this->success();
    }
}
